/**
 * 1.常用工具
 */

/**
 * 2.引入本地工具
 */
import {
	Storage
} from '../session/storage'
import {
	Utils
} from '../utils/utils'
import Config from '../config/config.js'
import {
	TestData
} from '../../test/testData'

/**
 * 3.引入其他service接口文件
 */
import '../service/login/loginService'

/**
 * 接口调用统一方法
 * @param option
 * @constructor
 */
function apiCall(option) {
	let api = option.api
	let url = api.url
	if (Utils.isNull(option.param) || Utils.isNull(option.param.requestParam)) {
		return uni.showToast({
			title: '请求参数不能为空'
		})
	}
	option.param.requestParam.token = Storage.getUserInfo().token
	if (api.type === 'upload') {
		uni.request(option)
	} else {
		uni.uploadFile(option)
	}
}

/**
 * 请求成功响应
 * @param errorObj
 */
function responseSuccess(data) {
	try {
		if (Utils.isNull(data.responseData.responseState.description)) {
			data.responseData.responseState.description = '网络繁忙，请稍后重试'
		}
	} catch (e) {}
	return data
}

/**
 * 请求失败响应
 * @param errorObj
 */
function responseFail(errorObj) {
	return {
		errorMsg: errorObj,
		responseData: {
			responseState: {
				code: 99,
				description: '网络繁忙，请稍后重试'
			}
		}
	}
}

const ApiCall = function(option) {
	if (Config.isDev && TestData.openTest) {
		setTimeout(function() {
			let data = TestData.getData(option.api.name)
			data.responseData.pageInfo = option.param.requestParam.pageInfo
			option.success(data)
		}, 1000)
		return
	}
	apiCall(option)
}
export {
	ApiCall
}
